package com.aysafety.core.common;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

public class PrimaryKeyUtils {

    private static Map<String, AtomicLong> counter = new HashMap<String, AtomicLong>();

    public static void register(String counterName, Long value) {
        if (value < 0) {
            value = 0L;
        }
        AtomicLong count = new AtomicLong(value);
        count.set(value);
        counter.put(counterName, count);
    }

    public static long getId(String counterName) {
        if (!counter.containsKey(counterName)) {
            return -1;
        }
        AtomicLong count = counter.get(counterName);
        return count.incrementAndGet();
    }

    public static long getMaxId(String counterName) {
        if (!counter.containsKey(counterName)) {
            return -1;
        }
        AtomicLong count = counter.get(counterName);
        return count.get();
    }
}
